home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GPExample / CGPNOTNode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-09  |  1.8 KB  |  91 lines

  1. //GPExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. ////////////////////////////////////////////////////////////
  6. //Only when compiling under Windows
  7. #include "stdafx.h"
  8. #define new DEBUG_NEW
  9. ////////////////////////////////////////////////////////////
  10.  
  11. #include "CGPNOTNode.h"
  12. #include "CGPNode.h"
  13. #include "CGP.h"
  14. #include "assert.h"
  15.  
  16. extern CGPNode *pGPCreateRandomSubtree(void);
  17.  
  18. extern CGPNode **pPrototypeList;
  19. extern unsigned long ulNumberOfPrototypes;
  20.  
  21. CGPNOTNode NOTPrototype;
  22.  
  23. CGPNOTNode::CGPNOTNode()
  24. {
  25.     unsigned long i;
  26.     nType=104;
  27.  
  28.     pChildren=NULL;
  29.     ulNumberOfChildren=1;
  30.  
  31.     sprintf(psName,"NOTNode");
  32.  
  33.     nIsPrototype=1;
  34.     if(AddToPrototypeList())
  35.     {
  36.         nIsPrototype=0;
  37.         pChildren=new CGPNode*[ulNumberOfChildren];
  38.         for(i=0;i<ulNumberOfChildren;i++)
  39.         {
  40.             pChildren[i]=NULL;
  41.         }
  42.     }
  43.     dPrior=0.05;
  44. }
  45.  
  46. CGPNOTNode::~CGPNOTNode()
  47. {
  48. }
  49.  
  50. CGPNode *CGPNOTNode::pGetCopy(CGP* pGP)
  51. {
  52.     unsigned long i;
  53.     CGPNOTNode *pNewNode=new CGPNOTNode;
  54.     assert(!(pNewNode==NULL));
  55.     for(i=0;i<ulNumberOfChildren;i++)
  56.     {
  57.         if(pNewNode->pChildren[i]!=NULL)
  58.         {
  59.             delete pNewNode->pChildren[i];
  60.         }
  61.         if(!nIsPrototype)
  62.         {
  63.             pNewNode->pChildren[i]=pChildren[i]->pGetCopy(pGP);
  64.         }
  65.         else
  66.         {
  67.             pNewNode->pChildren[i]=pGP->pGetRandomSubtree();
  68.         }
  69.     }
  70.     return (CGPNode*)pNewNode;
  71. }
  72.  
  73. double CGPNOTNode::dEvaluate(void)
  74. {
  75.     //This function return the arithmetical-logical NOT of the return value of its child
  76.     return 1.0-pChildren[0]->dEvaluate();
  77. }
  78.  
  79. char* CGPNOTNode::psGetString(char*pString)
  80. {
  81.     char *pSubString=new char[50000];
  82.     if(pString!=NULL && strlen(pString)<10000)
  83.     {
  84.         sprintf(pSubString,"");
  85.         pSubString=pChildren[0]->psGetString(pSubString);
  86.         sprintf(pString,"%s NOT(%s)",pString,pSubString);
  87.     }
  88.     delete []pSubString;
  89.     return pString;
  90. }
  91.